package com.web.work02_27Servlet;

import com.web.JDBCUtils;
import com.web.servlet.HelloServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteStudentByid")
public class DeleteStudentByid extends HelloServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置内容类型
        resp.setContentType("application/json;charset=UTF-8");
        //获取需要删除信息的id
        String id = req.getParameter("id");
        if (id != null && !id.equals("")) {
            if (JDBCUtils.update("delete from student where id=?",Integer.parseInt(id)) == 1){
                //删除成功
                resp.getWriter().write("msg:删除成功！");
            }else {
                //删除失败
                resp.getWriter().write("msg:删除失败！");
            }
        }else {
            resp.getWriter().write("msg:id为空！");
        }
    }
}